package com.leoman.utils;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.request.TimeGetRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.taobao.api.response.TimeGetResponse;

/**
 * 淘宝发送短信工具类
 * Created by 涂奕恒 on 2017/1/10 0010.
 */
public class SendMsgUtil {

    private static String url = "http://gw.api.taobao.com/router/rest";
    private static String appKey = "23511476";
    private static String appSecret = "ff6b6a86091349984426741644c96acd";

    public static void main(String[] args) {
        sendMsg("18717101121", "526985", null);
    }

    /**
     * 获取淘宝当前系统时间
     */
    public static void getTime() {
        try {
            DefaultTaobaoClient client = new DefaultTaobaoClient(url, appKey, appSecret);
            TimeGetRequest request = new TimeGetRequest();
            TimeGetResponse response = client.execute(request);
            if (response.isSuccess()) {
                System.out.println(response.getBody());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

    public static void sendMsg(String mobile, String code, Integer flag) {
        try {
            TaobaoClient client = new DefaultTaobaoClient(url, appKey, appSecret);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.setExtend("123456");
            req.setSmsType("normal");
            req.setSmsFreeSignName("52赫兹");
            req.setSmsParamString("{\"number\":\"" + code + "\"}");
            req.setRecNum(mobile);
            req.setSmsTemplateCode(null == flag ? "SMS_39195373" : "SMS_39175390");
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
}
